# -*- coding: utf-8 -*-
# @Time    : 2022/1/21 16:22
# @Author  : Mr.Ye
# @Email   : a3289748713@163.com
# @File    : models.py
# @Software: PyCharm

from MyVideo import db
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin

'''
插入数据之前重新构建
'''


# # 会员
# class User(db.Model):
#     __tablename__ = "user"
#     id = db.Column(db.Integer, primary_key=True)  # 编号
#     name = db.Column(db.String(100), unique=True)  # 昵称
#     pwd = db.Column(db.String(100))  # 密码
#     email = db.Column(db.String(100), unique=True)  # 邮箱
#     phone = db.Column(db.String(11), unique=True)  # 手机号码
#     info = db.Column(db.Text)  # 个性简介
#     face = db.Column(db.String(255), unique=True)  # 头像
#     addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 注册时间
#     uuid = db.Column(db.String(255), unique=True)  # 唯一标识符
#     userlogs = db.relationship('Userlog', backref='user')  # 会员日志外键关系关联
#     comments = db.relationship('Comment', backref='user')  # 评论外键关系关联
#     moviecols = db.relationship('Moviecol', backref='user')  # 收藏外键关系关联
#
#     def __repr__(self):
#         return "<User %r>" % self.name
#
#     def check_pwd(self, pwd):
#         from werkzeug.security import check_password_hash
#         return check_password_hash(self.pwd, pwd)
#
#
# # 会员登录日志
# class Userlog(db.Model):
#     __tablename__ = "userlog"
#     id = db.Column(db.Integer, primary_key=True)  # 编号
#     user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # 所属会员
#     ip = db.Column(db.String(100))  # 登录IP
#     addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 登录时间
#
#     def __repr__(self):
#         return "<Userlog %r>" % self.id
#
#
# # 标签
# class Tag(db.Model):
#     __tablename__ = "tag"
#     id = db.Column(db.Integer, primary_key=True)  # 编号
#     name = db.Column(db.String(100), unique=True)  # 标题
#     addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
#     movies = db.relationship('Movie', backref='tag')  # 电影外键关系关联
#
#     def __repr__(self):
#         return "<Tag %r>" % self.name
#
#
# # 电影
# class Movie(db.Model):
#     __tablename__ = "movie"
#     id = db.Column(db.Integer, primary_key=True)  # 编号
#     title = db.Column(db.String(255), unique=True)  # 标题
#     url = db.Column(db.String(255), unique=True)  # 地址
#     info = db.Column(db.Text)  # 简介
#     logo = db.Column(db.String(255), unique=True)  # 封面
#     star = db.Column(db.SmallInteger)  # 星级
#     playnum = db.Column(db.BigInteger)  # 播放量
#     commentnum = db.Column(db.BigInteger)  # 评论量
#     tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))  # 所属标签
#     area = db.Column(db.String(255))  # 上映地区
#     release_time = db.Column(db.Date)  # 上映时间
#     length = db.Column(db.String(100))  # 播放时间
#     addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
#     comments = db.relationship('Comment', backref='movie')  # 评论外键关系关联
#     moviecols = db.relationship('Moviecol', backref='movie')  # 收藏外键关系关联
#
#     def __repr__(self):
#         return "<Movie %r>" % self.title
#
#
# # 上映预告
# class Preview(db.Model):
#     __tablename__ = "preview"
#     id = db.Column(db.Integer, primary_key=True)  # 编号
#     title = db.Column(db.String(255), unique=True)  # 标题
#     logo = db.Column(db.String(255), unique=True)  # 封面
#     addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
#
#     def __repr__(self):
#         return "<Preview %r>" % self.title
#
#
# # 评论
# class Comment(db.Model):
#     __tablename__ = "comment"
#     id = db.Column(db.Integer, primary_key=True)  # 编号
#     content = db.Column(db.Text)  # 内容
#     movie_id = db.Column(db.Integer, db.ForeignKey('movie.id'))  # 所属电影
#     user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # 所属用户
#     addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
#
#     def __repr__(self):
#         return "<Comment %r>" % self.id
#
#
# # 电影收藏
# class Moviecol(db.Model):
#     __tablename__ = "moviecol"
#     id = db.Column(db.Integer, primary_key=True)
#     movie_id = db.Column(db.Integer, db.ForeignKey('movie.id'))  # 所属电影
#     user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # 所属用户
#     addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
#
#     def __repr__(self):
#         return "<Moviecol %r>" % self.id
#
# # 权限
# class Auth(db.Model):
#     __tablename__ = "auth"
#     id = db.Column(db.Integer, primary_key=True)  # 编号
#     name = db.Column(db.String(100), unique=True)  # 名称
#     url = db.Column(db.String(255), unique=True)  # 地址
#     addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
#
#     def __repr__(self):
#         return "<Auth %r>" % self.name
#
#
# 角色
class Role(db.Model):
    __tablename__ = "role"
    id = db.Column(db.Integer, primary_key=True)  # 编号,1
    name = db.Column(db.String(100), unique=True)  # 名称,超级管理员
    ad_group = db.Column(db.SmallInteger, unique=True)  # 角色组，超级管理员0,普通人员1。
    add_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
    admins = db.relationship('Admin', backref='role')  # 管理员外键关系关联

    def __repr__(self):
        return "<Role %r>" % self.name


# 管理员
class Admin(UserMixin,db.Model):
    __tablename__ = "admin"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    name = db.Column(db.String(100), unique=True)  # 管理员用户名
    pwd = db.Column(db.String(100))  # 管理员密码
    role_group = db.Column(db.SmallInteger, db.ForeignKey('role.ad_group'))  # 所属群组， # 是否为超级管理员,0为超级管理员
    add_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间

    # 相当于可以在AdminLog上传入admin的一个字段，由本表Admin来管理
    admin_logs = db.relationship('AdminLog', backref='admin')  # 管理员登录日志外键关系关联
    operation_logs = db.relationship('OperationLog', backref='admin')  # 管理员操作日志外键关系关联

    def __init__(self, **kwargs):
        super(Admin, self).__init__(**kwargs)


    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    @password.setter
    def password(self, password):
        self.pwd= generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.pwd, password)
    def __repr__(self):
        return '<Admin name: {}>'.format(self.name)

# 管理员登录日志
class AdminLog(db.Model):
    __tablename__ = "admin_log"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))  # 所属管理员
    ip = db.Column(db.String(100))  # 登录IP
    add_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 登录时间

    def __repr__(self):
        return "<admin_log %r>" % self.id

# 操作日志
class OperationLog(db.Model):
    __tablename__ = "op_log"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))  # 所属管理员
    ip = db.Column(db.String(100))  # 登录IP
    reason = db.Column(db.String(600))  # 操作原因
    add_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 登录时间

    def __repr__(self):
        return "<op_log %r>" % self.id
